package com.baizhi.service.impl;

import com.baizhi.dao.PmsCategoryMapper;
import com.baizhi.entity.PmsCategory;
import com.baizhi.service.IPmsCategoryService;
import com.baizhi.vo.CascaderNodeVo;
import com.baizhi.vo.LayuiTreeNode;
import com.baizhi.vo.ZTreeNode;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品三级分类 服务实现类
 * </p>
 *
 * @author lby
 * @since 2020-09-24
 */
@Service
public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryMapper, PmsCategory> implements IPmsCategoryService {

    @Override
    public List<LayuiTreeNode> getLayuiTreeNodes() {
        //获取所有的分类数据
        List<PmsCategory> categories = list();
        List<LayuiTreeNode> nodes = categories.stream().filter(
                pmsCategory -> {
                    return pmsCategory.getParentCid() == 0;
                })
                .map(pmsCategory -> {
                    LayuiTreeNode node = new LayuiTreeNode();
                    node.setId(pmsCategory.getCatId());
                    node.setTitle(pmsCategory.getName());
                    node.setChildren(getNodeChildren(node,categories));
                    return node;
                }).collect(Collectors.toList());
        return nodes;
    }

    private List<LayuiTreeNode> getNodeChildren(LayuiTreeNode node, List<PmsCategory> categories) {
        List<LayuiTreeNode> layuiTreeNodes = categories.stream().filter(pmsCategory -> {
            return node.getId() == pmsCategory.getParentCid();
        }).map(
                pmsCategory -> {
                    LayuiTreeNode treeNode = new LayuiTreeNode();
                    treeNode.setId(pmsCategory.getCatId());
                    treeNode.setTitle(pmsCategory.getName());
                    treeNode.setChildren(getNodeChildren(treeNode, categories));
                    return treeNode;
                }
        ).collect(Collectors.toList());
        return layuiTreeNodes;
    }

    @Override
    public List<ZTreeNode> getZTreeNodes() {
        List<PmsCategory> list = list();
        return list.stream()
                .filter(pmsCategory -> {
                    return pmsCategory.getParentCid() == 0;
                })
                .map(pmsCategory -> {
                    ZTreeNode node = new ZTreeNode();
                    /**
                     * 第一层级 设置为true
                     */
                    node.setFirstNode(true);
                    node.setId(pmsCategory.getCatId());
                    node.setPid(pmsCategory.getParentCid());
                    node.setName(pmsCategory.getName());
                    node.setChildren(getChildrenNode(node,list));
                    return node;
                }).collect(Collectors.toList());
    }
    private List<ZTreeNode> getChildrenNode(ZTreeNode node, List<PmsCategory> list) {
        return list.stream()
                .filter(pmsCategory -> {
                    return node.getId() == pmsCategory.getParentCid();
                })
                .map(pmsCategory -> {
                    ZTreeNode node1 = new ZTreeNode();
                    node1.setId(pmsCategory.getCatId());
                    node1.setPid(pmsCategory.getParentCid());
                    node1.setName(pmsCategory.getName());
                    node1.setChildren(getChildrenNode(node1,list));
                    return node1;
                }).collect(Collectors.toList());
    }

    @Override
    public List<CascaderNodeVo> getCascaderNodeVos() {
        List<PmsCategory> categories = list();
        return categories.stream()
                .filter(pmsCategory -> {
                    return pmsCategory.getParentCid() == 0;
                })
                .map(pmsCategory -> {
                    CascaderNodeVo vo = new CascaderNodeVo();
                    vo.setValue(pmsCategory.getCatId());
                    vo.setLabel(pmsCategory.getName());
                    vo.setChildren(getCascaderNodeVoChildren(vo, categories));
                    return vo;
                }).collect(Collectors.toList());
    }

    private List<CascaderNodeVo> getCascaderNodeVoChildren(CascaderNodeVo vo, List<PmsCategory> categories) {
        return categories.stream()
                .filter(pmsCategory -> {
                    return pmsCategory.getParentCid() == vo.getValue();
                })
                .map(pmsCategory -> {
                    CascaderNodeVo vo1 = new CascaderNodeVo();
                    vo1.setValue(pmsCategory.getCatId());
                    vo1.setLabel(pmsCategory.getName());
                    vo1.setChildren(getCascaderNodeVoChildren(vo1, categories));
                    return vo1;
                }).collect(Collectors.toList());
    }

}
